package com.java2007.hotel.controller.front;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.controller.BaseServlet;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.OrderService;
import com.java2007.hotel.service.front.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;

/**
 * 处理有关订单的请求
 * @author csl
 * @version V1.0
 * @date 2021/01/26
 **/
@WebServlet("/front/orderServlet")
public class FrontOrderServlet extends BaseServlet {
    private OrderService orderService = (OrderService) BeanFactory.getBean(Constant.ORDER_SERVICE_IMPL);
    private UserService userService = (UserService) BeanFactory.getBean(Constant.USER_SERVICE_IMPL);

    /**
     * 增加一个订单
     * @param req
     * @param resp
     * @return
     */
    public String add(HttpServletRequest req, HttpServletResponse resp){
        String tempTotalAllPrice = req.getParameter("totalAllPrice");
        long totalAllPrice = Long.parseLong(tempTotalAllPrice);

        String number = System.currentTimeMillis()+"";

        HttpSession session = req.getSession();
        Integer foodTableId = (Integer) session.getAttribute("foodTableId");
        User user = (User) session.getAttribute("user");

        Date date = new Date();
        Order order = new Order(null, number, foodTableId, totalAllPrice, (byte) 1, date, date, user.getId(), null);

        try {
            int i = orderService.addOrder(order);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        return "redirect:"+req.getContextPath()+"/front/detailServlet?method=toAccount&orderNumber="+order.getNumber();


    }

    /**
     * 结账
     * @param req
     * @param resp
     * @return
     */
    public String updateStatus(HttpServletRequest req, HttpServletResponse resp){

        String orderNumber = req.getParameter("orderNumber");
        String totalAllPrice = req.getParameter("totalAllPrice");
        return "back:"+"尊敬的顾客,您好!已经通知服务员结账，请稍等!%"+"front/orderServlet?method=settlement&totalAllPrice="+totalAllPrice+"&orderNumber="+orderNumber;
    }

    /**
     * 处理结账请求
     * @param req
     * @param resp
     * @return
     */
    public String settlement (HttpServletRequest req, HttpServletResponse resp){
        String tempTotalAllPrice = req.getParameter("totalAllPrice");
        String orderNumber = req.getParameter("orderNumber");
        long totalAllPrice = Long.parseLong(tempTotalAllPrice);

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user.getAccount() >= totalAllPrice){
            try {
                try {
                    orderService.updateOrderStatusByNumber((byte) 0, orderNumber);
                } catch (SQLException e) {
                    e.printStackTrace();
                    return "back:" + "服务器错误%"+"frontindex.jsp";
                }
                userService.updateAccountById(user.getId(),user.getAccount()-totalAllPrice);
            } catch (SQLException e) {
                e.printStackTrace();
                return "back:" + "服务器错误%"+"frontindex.jsp";
            }
            return "back:"+"结账成功%"+"front/tableServlet?method=selectAll";
        }else {
            session.removeAttribute("user");
            return "back:"+"余额不足，请充值后在下单%"+"frontindex.jsp";
        }



    }
}
